package com.king.cms.config;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.InstantiationException; 
import com.king.cms.common.*;
import com.king.cms.action.BaseDispatchAction;
import com.king.cms.log.LogService;
 

public class DisplayConfigAction extends BaseDispatchAction{
	private static final Log  log = LogFactory.getLog(DisplayConfigAction.class);
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws ServletException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws java.lang.InstantiationException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException {
		String action = request.getParameter("action");
		String meetingId = request.getParameter("meetingId");
		
		if("view".equals(action)){
			List data = this.getDBOperator(request).select("select * from cms_meeting where id=?", new Object[]{meetingId});
			Map m = (Map)data.get(0);
			String t = m.get("TIME_DISPLAY_CONFIG")+"";
			String[] tt = null;
			if(t!=null && !"null".equals(t)){
				tt = t.split("@");
				request.setAttribute("time_c", null2Blank(tt[0]));
				request.setAttribute("time_f", null2Blank(tt[1]));
				request.setAttribute("time_s", null2Blank(tt[2]));
			}
			
			t = m.get("WEEK_DISPLAY_CONFIG")+"";
			if(t!=null && !"null".equals(t)){
				tt = t.split("@");
				request.setAttribute("week_c", null2Blank(tt[0]));
				request.setAttribute("week_f", null2Blank(tt[1]));
				request.setAttribute("week_s", null2Blank(tt[2]));
			}
			
			t = m.get("DATE_DISPLAY_CONFIG")+"";
			if(t!=null && !"null".equals(t)){
				tt = t.split("@");
				request.setAttribute("date_c", null2Blank(tt[0]));
				request.setAttribute("date_f", null2Blank(tt[1]));
				request.setAttribute("date_s", null2Blank(tt[2]));
			}
			
			t = m.get("LINE1_DISPLAY_CONFIG")+"";
			if(t!=null && !"null".equals(t)){
				tt = t.split("@");
				request.setAttribute("line1_c", null2Blank(tt[0]));
				request.setAttribute("line1_f", null2Blank(tt[1]));
				request.setAttribute("line1_s", null2Blank(tt[2]));
			}
			t = m.get("LINE2_DISPLAY_CONFIG")+"";
			if(t!=null && !"null".equals(t)){
				tt = t.split("@");
				request.setAttribute("line2_c", null2Blank(tt[0]));
				request.setAttribute("line2_f", null2Blank(tt[1]));
				request.setAttribute("line2_s", null2Blank(tt[2]));
			}
			t = m.get("LINE3_DISPLAY_CONFIG")+"";
			if(t!=null && !"null".equals(t)){
				tt = t.split("@");
				
				request.setAttribute("line3_c", null2Blank(tt[0]));
				request.setAttribute("line3_f", null2Blank(tt[1]));
				request.setAttribute("line3_s", null2Blank(tt[2]));
			}
			t = m.get("LINE3_DISPLAY_CONFIG")+"";
			if(t!=null && !"null".equals(t)){
				tt = t.split("@");
				request.setAttribute("line4_c", null2Blank(tt[0]));
				request.setAttribute("line4_f", null2Blank(tt[1]));
				request.setAttribute("line4_s", null2Blank(tt[2]));
			}
			request.setAttribute("meetingId", meetingId);
			return mapping.findForward("display");
		}
		if("save".equals(action)){
			String time  = blank2Null(request.getParameter("time_c"))+"@"+blank2Null(request.getParameter("time_f"))+"@"+blank2Null(request.getParameter("time_s"))+"@";
			String week  = blank2Null(request.getParameter("week_c"))+"@"+blank2Null(request.getParameter("week_f"))+"@"+blank2Null(request.getParameter("week_s"))+"@";
			String date  = blank2Null(request.getParameter("date_c"))+"@"+blank2Null(request.getParameter("date_f"))+"@"+blank2Null(request.getParameter("date_s"))+"@";
			String line1  = blank2Null(request.getParameter("line1_c"))+"@"+blank2Null(request.getParameter("line1_f"))+"@"+blank2Null(request.getParameter("line1_s"))+"@";
			String line2  = blank2Null(request.getParameter("line2_c"))+"@"+blank2Null(request.getParameter("line2_f"))+"@"+blank2Null(request.getParameter("line2_s"))+"@";
			String line3 = blank2Null(request.getParameter("line3_c"))+"@"+blank2Null(request.getParameter("line3_f"))+"@"+blank2Null(request.getParameter("line3_s"))+"@";
			String line4  = blank2Null(request.getParameter("line4_c"))+"@"+blank2Null(request.getParameter("line4_f"))+"@"+blank2Null(request.getParameter("line4_s"))+"@";
			String uid = SessionUtil.getUserId(request)+"";
			 
			request.setAttribute("time_c", request.getParameter("time_c"));
			request.setAttribute("time_f", request.getParameter("time_f"));
			request.setAttribute("time_s", request.getParameter("time_s"));
			
			request.setAttribute("week_c", request.getParameter("week_c"));
			request.setAttribute("week_f", request.getParameter("week_f"));
			request.setAttribute("week_s", request.getParameter("week_s"));
			
			request.setAttribute("date_c", request.getParameter("date_c"));
			request.setAttribute("date_f", request.getParameter("date_f"));
			request.setAttribute("date_s", request.getParameter("date_s"));
			
			request.setAttribute("line1_c", request.getParameter("line1_c"));
			request.setAttribute("line1_f", request.getParameter("line1_f"));
			request.setAttribute("line1_s", request.getParameter("line1_s"));
			
			request.setAttribute("line2_c", request.getParameter("line2_c"));
			request.setAttribute("line2_f", request.getParameter("line2_f"));
			request.setAttribute("line2_s", request.getParameter("line2_s"));
			
			
			request.setAttribute("line3_c", request.getParameter("line3_c"));
			request.setAttribute("line3_f", request.getParameter("line3_f"));
			request.setAttribute("line3_s", request.getParameter("line3_s"));
			
			request.setAttribute("line4_c", request.getParameter("line4_c"));
			request.setAttribute("line4_f", request.getParameter("line4_f"));
			request.setAttribute("line4_s", request.getParameter("line4_s"));
			HttpSession session = request.getSession();
			 
			if(null != session)
			   session.setAttribute(meetingId, time+"&"+week+"&"+date+"&"+line1+"&"+line2+"&"+line3+"&"+line4+"&");
			System.out.println(session.getAttribute(meetingId));
			request.setAttribute("meetingId", meetingId);
			request.setAttribute("script", "window.close();");
			return mapping.findForward("display");
		}
		return mapping.findForward("logon.fail");
	 
	}
	private String blank2Null(String s){
		if("".equals(s))
			return "null";
	return s;
	}
	private String null2Blank(String s){
		if("null".equals(s))
			return "";
	return s;
	}
}
